package com.winning.devops.websocket.web;

import com.winning.devops.websocket.model.SocketMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author chensj
 * @title
 * @project spring-boot-cli
 * @package com.winning.devops.websocket.web
 * @date: 2019-04-03 21:20
 */
@Controller
public class AppController {

    /**
     * websochet消息发送对象
     */
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    /**
     * 跳转到测试页面
     * @return
     */
    @GetMapping("/test")
    public String index() {
        return "test01";
    }

    //测试页面显示后台消息推送次数
    private int count=0;

    //接收浏览器消息路径设置
    @MessageMapping("/send")
    //服务端向浏览器推送地址设置
    @SendTo("/topic/send")
    public SocketMessage send(SocketMessage message) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        message.date = "返回 信息："+df.format(new Date());
        return message;
    }

    //由后台发送到浏览器服务
    @SendTo("/topic/callback")
    //定时5秒给页面推一次数据
    @Scheduled(cron="0/5 * * * * ?")
    public Object callback() throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("推送消息了"+df.format(new Date()));
        //向页面这个地址推送消息
        messagingTemplate.convertAndSend("/topic/callback","客户端消息"+count );
        count++;
        return null;
    }

}

